package com.zg.scf.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zg.common.core.R;
import com.zg.scf.domain.req.AlertIgnoreAddReq;
import com.zg.scf.domain.req.AlertPageReq;
import com.zg.scf.domain.req.AlertProcessAddReq;
import com.zg.scf.domain.req.AlertServiceDueEditReq;
import com.zg.scf.domain.vo.CompanyBusinessAlertVo;
import com.zg.scf.service.CompanyBusinessAlertService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@Api(tags = {"企业和业务预警"})
@RestController
@RequiredArgsConstructor
public class CompanyBusinessAlertController {

    private final CompanyBusinessAlertService companyBusinessAlertService;

    @ApiOperation("分页查询服务到期预警")
    @PostMapping("/companyBusinessAlert/serviceDue/page")
    public R<IPage<CompanyBusinessAlertVo>> serviceDuePage(@RequestBody @Valid AlertPageReq req) {
        IPage<CompanyBusinessAlertVo> page = companyBusinessAlertService.serviceDuePage(req);
        return R.success(page);
    }

    @ApiOperation("分页查询长期失活预警")
    @PostMapping("/companyBusinessAlert/longTermInactivation/page")
    public R<IPage<CompanyBusinessAlertVo>> longTermInactivationPage(
            @RequestBody @Valid AlertPageReq req) {
        IPage<CompanyBusinessAlertVo> page = companyBusinessAlertService.longTermInactivation(req);
        return R.success(page);
    }

    @ApiOperation("分页查询无业务预警")
    @PostMapping("/companyBusinessAlert/noBusiness/page")
    public R<IPage<CompanyBusinessAlertVo>> noBusinessPage(@RequestBody @Valid AlertPageReq req) {
        IPage<CompanyBusinessAlertVo> page = companyBusinessAlertService.noBusinessPage(req);
        return R.success(page);
    }

    @ApiOperation("分页查询违规预警")
    @PostMapping("/companyBusinessAlert/violation/page")
    public R<IPage<CompanyBusinessAlertVo>> violationPage(@RequestBody @Valid AlertPageReq req) {
        IPage<CompanyBusinessAlertVo> page = companyBusinessAlertService.violationPage(req);
        return R.success(page);
    }

    @ApiOperation("分页查询授信额度异常预警")
    @PostMapping("/companyBusinessAlert/creditAmountError/page")
    public R<IPage<CompanyBusinessAlertVo>> creditAmountErrorPage(@RequestBody @Valid AlertPageReq req) {
        IPage<CompanyBusinessAlertVo> page = companyBusinessAlertService.creditAmountErrorPage(req);
        return R.success(page);
    }

    @ApiOperation("分页查询融资金额异常预警")
    @PostMapping("/companyBusinessAlert/financingAmountError/page")
    public R<IPage<CompanyBusinessAlertVo>> financingAmountErrorPage(@RequestBody @Valid AlertPageReq req) {
        IPage<CompanyBusinessAlertVo> page = companyBusinessAlertService.financingAmountErrorPage(req);
        return R.success(page);
    }

    @ApiOperation("分页查询协议异常预警")
    @PostMapping("/companyBusinessAlert/agreementError/page")
    public R<IPage<CompanyBusinessAlertVo>> agreementErrorPage(@RequestBody @Valid AlertPageReq req) {
        IPage<CompanyBusinessAlertVo> page = companyBusinessAlertService.agreementErrorPage(req);
        return R.success(page);
    }

    @ApiOperation("分页查询还贷异常预警")
    @PostMapping("/companyBusinessAlert/repaymentError/page")
    public R<IPage<CompanyBusinessAlertVo>> repaymentErrorPage(@RequestBody @Valid AlertPageReq req) {
        IPage<CompanyBusinessAlertVo> page = companyBusinessAlertService.repaymentErrorPage(req);
        return R.success(page);
    }

    @ApiOperation("处理预警")
    @PostMapping("/companyBusinessAlert/process/add")
    public R<Void> processAdd(@RequestBody @Valid AlertProcessAddReq req) {
        companyBusinessAlertService.processAdd(req);
        return R.success();
    }

    @ApiOperation("服务延期")
    @PostMapping("/companyBusinessAlert/serviceDue/edit")
    public R<Void> serviceDueEdit(@RequestBody @Valid AlertServiceDueEditReq req) {
        companyBusinessAlertService.serviceDueEdit(req);
        return R.success();
    }

    @ApiOperation("企业注销")
    @GetMapping("/companyBusinessAlert/companyLogout/add")
    public R<Void> companyLogoutAdd(Long id) {
        if (id == null) {
            return R.error("请选择预警数据");
        }
        companyBusinessAlertService.companyLogoutAdd(id);
        return R.success();
    }

    @ApiOperation("忽略")
    @PostMapping("/companyBusinessAlert/ignore/add")
    public R<Void> ignoreAdd(@RequestBody @Valid AlertIgnoreAddReq req) {
        companyBusinessAlertService.ignoreAdd(req);
        return R.success();
    }

    @ApiOperation("调整授信额度")
    @GetMapping("/companyBusinessAlert/creditAmount/edit")
    public R<Void> creditAmountEdit(Long id) {
        if (id == null) {
            return R.error("请选择预警数据");
        }
        companyBusinessAlertService.creditAmountEdit(id);
        return R.success();
    }

    @ApiOperation("更新融资额度")
    @GetMapping("/companyBusinessAlert/financingAmount/edit")
    public R<Void> financingAmountEdit(Long id) {
        if (id == null) {
            return R.error("请选择预警数据");
        }
        companyBusinessAlertService.financingAmountEdit(id);
        return R.success();
    }

    @ApiOperation("签署")
    @GetMapping("/companyBusinessAlert/agreement/add")
    public R<Void> agreementAdd(Long id) {
        if (id == null) {
            return R.error("请选择预警数据");
        }
        companyBusinessAlertService.agreementAdd(id);
        return R.success();
    }

    @ApiOperation("还款状态回退")
    @GetMapping("/companyBusinessAlert/repayment/edit")
    public R<Void> repaymentEdit(Long id) {
        if (id == null) {
            return R.error("请选择预警数据");
        }
        companyBusinessAlertService.repaymentEdit(id);
        return R.success();
    }

    /**
     * 定时判断企业和业务预警
     */
//    @Scheduled(cron = "0 0 1 * * ?")
    void handleAlert() {
        // 判断服务到期预警
        companyBusinessAlertService.serviceDueAdd();

        // 判断长期失活预警
        companyBusinessAlertService.longTermInactivationAdd();

        // 判断无业务预警
        companyBusinessAlertService.noBusinessAdd();

        // 判断无业务预警
        companyBusinessAlertService.violationAdd();

        // 判断授信额度异常预警
        companyBusinessAlertService.creditAmountErrorAdd();

        // 判断融资金额异常预警
        companyBusinessAlertService.financingAmountErrorAdd();

        // 判断协议异常预警
        companyBusinessAlertService.agreementErrorAdd();

        // 判断还贷异常预警
        companyBusinessAlertService.repaymentErrorAdd();
    }
}
